/*
 * @Author: C.
 * @Date: 2023-02-24 10:12:38
 */
/*
 * @Author: C.
 * @Date: 2023-02-24 10:12:38
 */
import { number2String } from './utils';
const cellList = ['summary', 'ledger', 'debit', 'creditor'];
interface ICellTarget {
  cellObj: any;
  level: null | number;
}
class Cell {
  public inputShow = false;
  public value: string | number = '';
  public type = 'summary';
  public row: any = null;
  constructor(param) {
    this.type = param.type;
    this.row = param.row;
  }
  get display(): string | string[] | number {
    return this.value;
  }
  public getNextCell = (list, currentCell) => {
    const target: ICellTarget = {
      cellObj: null,
      level: null,
    };
    const index = cellList.indexOf(this.type);
    if (currentCell.level === list.value.length - 1 && index + 1 === cellList.length) {
      target.cellObj = list.value[0][cellList[0]];
      target.level = 0;
      return target;
    }
    if (index + 2 <= cellList.length) {
      target.cellObj = list.value[currentCell.level][cellList[index + 1]];
      target.level = currentCell.level;
    } else {
      target.cellObj = list.value[currentCell.level + 1][cellList[0]];
      target.level = currentCell.level + 1;
    }
    return target;
  };
  public getRightCell = (list, currentCell) => {
    const target: ICellTarget = {
      cellObj: null,
      level: null,
    };
    const index = cellList.indexOf(this.type);
    // console.log(index);

    if (currentCell.level === 0 && index === 0) {
      target.cellObj = list.value[list.value.length - 1][cellList[cellList.length - 1]];
      target.level = list.value.length - 1;
      return target;
    }

    if (index - 1 >= 0) {
      target.cellObj = list.value[currentCell.level][cellList[index - 1]];
      target.level = currentCell.level;
    } else {
      target.cellObj = list.value[currentCell.level - 1][cellList[cellList.length - 1]];
      target.level = currentCell.level - 1;
    }
    return target;
  };
  public getDownCell = (list, currentCell) => {
    const target: ICellTarget = {
      cellObj: null,
      level: null,
    };
    // if(currentCell.level)
    if (currentCell.level + 1 > list.value.length - 1) {
      target.cellObj = list.value[0][this.type];
      target.level = 0;
    } else {
      target.cellObj = list.value[currentCell.level + 1][this.type];
      target.level = currentCell.level + 1;
    }
    return target;
  };
  public getUpCell = (list, currentCell) => {
    const target: ICellTarget = {
      cellObj: null,
      level: null,
    };
    // if(currentCell.level)
    if (currentCell.level - 1 < 0) {
      console.log(currentCell.level);

      target.cellObj = list.value[list.value.length - 1][this.type];
      target.level = list.value.length - 1;
    } else {
      target.cellObj = list.value[currentCell.level - 1][this.type];
      target.level = currentCell.level - 1;
    }
    return target;
  };
}
class Content extends Cell {
  constructor(param) {
    super(param);
  }
}
class Amount extends Cell {
  constructor(param) {
    super(param);
    this.value = 0;
  }
  get display(): string | string[] {
    return number2String(this.value);
  }
}
class Row {
  // 摘要
  summary = new Content({ type: 'summary', row: this });
  // 会计科目
  ledger = new Content({ type: 'ledger', row: this });
  // 借方
  debit = new Amount({ type: 'debit', row: this });
  // 贷方
  creditor = new Amount({ type: 'creditor', row: this });
}
export function creatNewRow() {
  return new Row();
}
